package main

func main() {

	a := matrixReshape([][]int{{1, 2}, {3, 4}}, 3, 2)

	for _, v := range a {
		for _, w := range v {
			print(w)
		}
		println()
	}
}

func matrixReshape(mat [][]int, r int, c int) [][]int {

	if len(mat) == 0 {
		return mat
	}

	// 判断是否能转
	if len(mat)*len(mat[0]) != r*c {
		return mat
	}

	// 一个新的矩阵
	result := [][]int{}
	sub := []int{}

	for i := 0; i < len(mat); i++ {
		for j := 0; j < len(mat[i]); j++ {
			// 往新矩阵丢数据

			sub = append(sub, mat[i][j])
			if len(sub) == c {
				result = append(result, sub)
				sub = []int{}
			}
		}
	}

	return result
}
